<?php


namespace app\admin\model;


use app\common\traits\ActionRecordTrait;

/**
 * Class Slideshow
 * @package app\admin\model
 * @author Mr.Liu Wuchen
 * @create 2021-06-24 16:31
 */
class Slideshow extends Common
{

    use ActionRecordTrait;

    /**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如CRM模块用crm作为数据表前缀
     */
    protected $name = 'admin_slideshow';

    /**
     * 保存
     * @param  array $request 请求参数
     * @return bool
     * @author Mr.Liu Wuchen
     * @create 2021-06-24 16:35
     */
    public function saveData(array $request): bool
    {
        try {
            $userId = $request['user_id'];
            unset($request['user_id']);
            if (!$request) {
                $this->error = '请上传图片';
                return false;
            }
            $dataAll = [];
            foreach ($request as $key => $val) {
                $image = trim($val['image']);
                if (!$image) {
                    continue;
                }
                $dataAll[] = [
                    'image'     => $image,
                    'jump_type' => $val['jump_type'] ?? 0,
                    'value'     => isset($val['value']) && $val['value'] ? $val['value'] : ''
                ];
            }
            if (!$dataAll) {
                $this->error = '请上传图片';
                return false;
            }
            // 删除原数据
            $this->where('id', '>', 0)->delete();
            // 添加新数据
            $insertNum = $this->insertAll($dataAll);
            if ($insertNum > 0) {
                // 操作记录
                $logCode       = json_encode($dataAll, JSON_UNESCAPED_UNICODE);
                $logContentStr = "更新了轮播图";
                $this->addActionRecord($userId, 'admin_slideshow_update', 0, $logContentStr, 'admin', 'slideshow', $logCode);
                $this->error = '保存成功';
                return true;
            }
        } catch (\Exception $e) {
        }

        $this->error = '保存失败';
        return false;
    }

    /**
     * 信息
     * @param  array $request 请求参数
     * @return array
     * @author Mr.Liu Wuchen
     * @create 2021-06-24 16:32
     */
    public function getDataInfo(array $request): array
    {
        try {
            $where = [];
            $field = [];
            $list  = $this->where($where)->field($field)->select()->toArray();
            foreach ($list as $key => $val) {
                $list[$key]['image_path'] = config('app.img_url').$val['image'];
            }

            $this->error = '获取信息成功';
            return $list;
        } catch (\Exception $e) {
        }

        $this->error = '获取信息失败';
        return [];
    }

    /**
     * 跳转列表
     * @param  array $request 请求参数
     * @return array
     * @author Mr.Liu Wuchen
     * @create 2021-06-23 17:01
     */
    public function getJumpValueDataList(array $request): array
    {
        try {
            $field       = ['article_id' => 'id', 'title' => 'name'];
            $articleList = (new \app\admin\model\Article())->getArticleList([], $field);
            $where = [
                ['status', '=', '1']
            ];
            $field       = ['id', 'name'];
            $goodsList   = (new \app\shop\model\Goods())->getGoodsList($where, $field);

            $list = ['1' => $articleList ?: [], '2' => $goodsList ?: []];


            // $jumpType = $request['jump_type'] ?? 0;
            // switch ($jumpType) {
            //     case '1':
            //         $field = ['article_id' => 'id', 'title' => 'name'];
            //         $list = (new \app\admin\model\Article())->getArticleList([], $field);
            //         break;
            //     case '2':
            //         $where = [
            //             ['status', '=', '1']
            //         ];
            //         $field = ['id', 'name'];
            //         $list  = (new \app\shop\model\Goods())->getGoodsList($where, $field);
            //         break;
            //     default:
            //         $list = [];
            //         break;
            // }

            $data['data'] = $list ?: [];
            return $data;
        } catch (\Exception $e) {
        }

        $this->error = '暂无分类数据';
        return [];
    }

}